<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换人" prop="userId">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入兑换人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="exchangeStatus" style="margin-left: 20px">
        <el-select v-model="queryParams.exchangeStatus" placeholder="请选择订单状态" style="">
          <el-option
            v-for="item in status_options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['supermarket:exchange:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['supermarket:exchange:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['supermarket:exchange:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['supermarket:exchange:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exchangeList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <div v-for="(item,index) in props.row.arExchangeItemVOs" style="width: 40%;margin-left: 5%;margin-top: 20px">
            <div style="display: flex">
              <div style="margin: 10px 10px 0 0">{{ index + 1 }}.</div>
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="商品名称：">
                  <span>{{ item.productName }}</span>
                </el-form-item>
                <el-form-item label="颜色：">
                  <span>{{ item.color }}</span>
                </el-form-item>
                <el-form-item label="规格：">
                  <span>{{ item.specifications }}</span>
                </el-form-item>
                <el-form-item label="数量：">
                  <span>{{ item.productNumber }}</span>
                </el-form-item>
                <el-form-item label="单价：">
                  <span>{{ item.monovalent }}</span>
                </el-form-item>
                <el-form-item label="总价格：">
                  <span>{{ item.totalPrice }}</span>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </template>
      </el-table-column>
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column type="index" width="50" fixed></el-table-column>
      <!--      <el-table-column label="兑换记录id" align="center" prop="id" />-->
      <el-table-column label="兑换人" align="center" prop="username" width="120"/>
      <!--      <el-table-column label="商品名称" align="center" prop="productName" width="200"/>-->
      <!--      <el-table-column label="单价" align="center" prop="monovalent" width="100"/>-->
      <!--      <el-table-column label="商品数量" align="center" prop="productNumber" width="100"/>-->
      <el-table-column label="总价" align="center" prop="totalPrice" width="120"/>
      <!--      <el-table-column label="颜色" align="center" prop="color" width="150"/>-->
      <!--      <el-table-column label="尺码" align="center" prop="size" width="120"/>-->
      <!--      <el-table-column label="规格" align="center" prop="specifications" width="150"/>-->
      <!--      <el-table-column label="时间段" align="center" prop="timeSlot" width="150"/>-->
      <!--      <el-table-column label="时间区间" align="center" prop="timeInterval" width="150"/>-->
      <el-table-column label="兑换状态" align="center" prop="exchangeStatus" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.exchangeStatus==0" style="color: red">待取</span>
          <span v-if="scope.row.exchangeStatus==1">已取</span>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="兑换地点" align="center" prop="exchangeLocation" width="180"/>
      <el-table-column label="兑换时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
                    <el-button
                      v-if="scope.row.exchangeStatus==0"
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['supermarket:exchange:edit']"
                    >确认兑换
                    </el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['supermarket:exchange:remove']"
                    >删除
                    </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改兑换记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="单价" prop="monovalent">
          <el-input v-model="form.monovalent" placeholder="请输入单价"/>
        </el-form-item>
        <el-form-item label="商品数量" prop="productNumber">
          <el-input v-model="form.productNumber" placeholder="请输入商品数量"/>
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总价"/>
        </el-form-item>
        <el-form-item label="人员id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入人员id"/>
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-input v-model="form.color" placeholder="请输入颜色"/>
        </el-form-item>
        <el-form-item label="尺码" prop="size">
          <el-input v-model="form.size" placeholder="请输入尺码"/>
        </el-form-item>
        <el-form-item label="规格" prop="specifications">
          <el-input v-model="form.specifications" placeholder="请输入规格"/>
        </el-form-item>
        <el-form-item label="时间段" prop="timeSlot">
          <el-input v-model="form.timeSlot" placeholder="请输入时间段"/>
        </el-form-item>
        <el-form-item label="时间区间" prop="timeInterval">
          <el-input v-model="form.timeInterval" placeholder="请输入时间区间"/>
        </el-form-item>
        <el-form-item label="商品首图" prop="indexImg">
          <el-input v-model="form.indexImg" placeholder="请输入商品首图"/>
        </el-form-item>
        <el-form-item label="兑换地点" prop="exchangeLocation">
          <el-input v-model="form.exchangeLocation" placeholder="请输入兑换地点"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExchange,
  getExchange,
  delExchange,
  addExchange,
  updateExchange,
  patchExchange
} from '@/api/supermarket/exchange'

export default {
  name: 'Exchange',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 兑换记录表格数据
      exchangeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        realName: null,
        username:null,
        exchangeStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //订单状态
      status_options: [{
        value: null,
        label: '全部'
      }, {
        value: '0',
        label: '待取'
      }, {
        value: '1',
        label: '已取'
      }]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询兑换记录列表 */
    getList() {
      this.loading = true
      listExchange(this.queryParams).then(response => {
        this.exchangeList = response.pageData
        console.log(this.exchangeList)
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        monovalent: null,
        productNumber: null,
        totalPrice: null,
        userId: null,
        color: null,
        size: null,
        specifications: null,
        timeSlot: null,
        timeInterval: null,
        exchangeStatus: null,
        createTime: null,
        indexImg: null,
        exchangeLocation: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加兑换记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      patchExchange(id).then(response => {
        this.getList()
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExchange(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addExchange(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除改订单').then(function() {
        return delExchange(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('supermarket/exchange/export', {
        ...this.queryParams
      }, `exchange_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 30%;
}
</style>
